import 'package:flutter/material.dart';

const MAX_LINES = 3;

// 没超过三行的测试文本
const TEXT = "aaa\n1aaaa\n1111111111111111111111222222111111111";

// 超过三行的测试文本
const TEXT1 = "aaa\n1aaaa\n111111111111111111111122222211111111111";

const TEXT_STYLE = TextStyle(fontSize: 17, color: Colors.blue);

class TestTextExpand extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test text expand"),
      ),
      body: Container(
        child: isOverflow(TEXT1, TEXT_STYLE, MAX_LINES, MediaQuery.of(context).size.width)
            ? Text("超过$MAX_LINES行了")
            : Text(TEXT, style: TEXT_STYLE),
      ),
    );
  }

  bool isOverflow(String text, TextStyle style, int maxLines, double maxWidth) {
    bool isOverflow = false;
    final span = TextSpan(text: text, style: style);
    final tp = TextPainter(text: span, maxLines: maxLines, textDirection: TextDirection.ltr);
    tp.layout(maxWidth: maxWidth);
    if (tp.didExceedMaxLines) {
      isOverflow = true;
    } else {
      isOverflow = false;
    }
    return isOverflow;
  }
}
